package com.cuanbo.dualscreenteachfu.utils;

import android.util.Log;

import com.cuanbo.dualscreenteachfu.base.App;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

public class DateUtil {
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    private static final SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy.MM.dd   EEEE   HH:mm");
    private static final SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat sdfHH = new SimpleDateFormat("HH");
    private static final SimpleDateFormat sdfmm = new SimpleDateFormat("mm");
    private static final SimpleDateFormat sdfss = new SimpleDateFormat("ss");
    private static final SimpleDateFormat sdf5 = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat sdf6 = new SimpleDateFormat("hh:mm:ss");

    /**
     * 获取当前时间的格式化字符串：2021.07.09 星期五 09:12
     */
    public static String fmtNowDate1() {
        return sdf1.format(new Date());
    }

    /**
     * 获取当前时间的格式化字符串：2021-07-09 09:12:45
     */
    public static String fmt(long time) {
        return sdf2.format(time);
    }

    public static String fmtNowDate2() {
        return sdf2.format(new Date());
    }

    public static String fmtYMD() {
        return sdf.format(new Date());
    }

    public static String fmtYMD(long time) {
        return sdf.format(time);
    }

    public static String getNowyyyy_MM_dd() {
        return sdf5.format(new Date());
    }

    public static String getNowHMS() {
        return sdf6.format(new Date());
    }

    public static String getHMS(long millis) {
        int seconds=(int)(millis/1000);
        int hours = seconds / 3600; // 计算小时数
        int remainder = seconds % 3600; // 计算剩余秒数
        int minutes = remainder / 60; // 计算分钟数
        seconds = remainder % 60; // 计算剩余秒数
        // 使用String.format进行格式化，确保单个数字前补零
        return String.format("%02d:%02d:%02d", hours, minutes, seconds);
    }

    public static String getSysHMS(){
        if(App.sysTime==0){
            return "[]";
        }
        return "["+sdf2.format(App.sysTime)+"]";
    }

    public static long getSDF2(String strData) {
        try {
            return sdf2.parse(strData).getTime();
        } catch (ParseException e) {
        }
        return 0;
    }

    public static int getHourHigh() {
        Calendar calendar=Calendar.getInstance();
        int hours=calendar.get(Calendar.HOUR_OF_DAY);
        return hours/10;
    }

    public static int getHourLow() {
        Calendar calendar=Calendar.getInstance();
        int hours=calendar.get(Calendar.HOUR_OF_DAY);
        return hours%10;
    }

    public static int getMinuteHigh() {
        Calendar calendar=Calendar.getInstance();
        int minutes=calendar.get(Calendar.MINUTE);
        return minutes/10;
    }

    public static int getMinuteLow() {
        Calendar calendar=Calendar.getInstance();
        int minutes=calendar.get(Calendar.MINUTE);
        return minutes%10;
    }

    public static int getHour(long date) {
        try {
            int hour= Integer.valueOf(sdfHH.format(new Date(date)));
            Log.i("luohao",""+hour);
            return hour;
        } catch (Exception e) {

        }
        return -1;
    }

    public static int getMinute(long date) {
        try {
            return Integer.valueOf(sdfmm.format(new Date(date)));
        } catch (Exception e) {

        }
        return -1;
    }

    public static int getSecond(long date) {
        try {
            return Integer.valueOf(sdfss.format(new Date(date)));
        } catch (Exception e) {

        }
        return -1;
    }

    /**
     * 传入值与当前时间的差值
     */
    public static boolean nowDiff(long time, long maxDiff) {
        if (new Date().getTime() - time > maxDiff) {
            return false;
        } else {
            return true;
        }
    }

    public static String getNowTimeFmt(String timeFmt) {
        String str = "时间格式化异常";
        try {
            str = new SimpleDateFormat(timeFmt).format(new Date());
        } catch (Exception e) {
            LogUtil.addError("---------------------当前时间格式化异常-------------------------");
            LogUtil.addError(e);
            LogUtil.addError("---------------------------------------------------------------");
        }
        return str;
    }

    /**
     * 当前时间戳+3位随机数
     */
    public static String getUniqueNumber() {
        String str = "";
        str += new Date().getTime();
        str += new Random().nextInt(10);
        str += new Random().nextInt(10);
        str += new Random().nextInt(10);
        return str;
    }
}
